const ProductModel = require("../../models/ProductModel");

const ProductService = {
  addProduct: async ({ title, introduction, detail, cover, productTime }) => {
    return ProductModel.create({
      title,
      introduction,
      detail,
      cover,
      productTime,
    });
  },
  getProductList: async ({ _id }) => {
    return _id ? ProductModel.find({ _id }) : ProductModel.find();
  },
  deleteProduct: async ({ _id }) => {
    return ProductModel.deleteOne({ _id });
  },
  updateProduct: async ({
    _id,
    title,
    introduction,
    detail,
    cover,
    productTime,
  }) => {
    if (cover) { 
      return ProductModel.updateOne(
        { _id },
        {
          title,
          introduction,
          detail,
          cover,
          productTime,
        }
      );
    } else {
      return ProductModel.updateOne(
        { _id },
        {
          title,
          introduction,
          detail,
          productTime,
        }
      );
    }
  },
};

module.exports = ProductService;
